<template>
  <div>
    <el-badge :value="todoNum" :max="99" class="item">
      <svg-icon icon-class="message"  />
    </el-badge>
  </div>
</template>

<script>
import { listAnnouncement } from '@/api/system/announcement'
import Cookies from "js-cookie";


export default {
  name: 'RuoYiGit',
  data() {
    return {
      todoNum:1,
    }
  },
  created() {
    this.getCount()
    this.connectWebsocket();
  },
  methods: {
    getCount(){
      var data = new Object();
      data.status="0"
      listAnnouncement(data).then(response => {
        this.todoNum = response.total
      })
    },
    connectWebsocket() {
      const username = Cookies.get("username");
      let websocket;
      if (typeof WebSocket === "undefined") {
        console.log("您的浏览器不支持WebSocket");
        return;
      } else {
        let protocol = "ws";
        let url = "";
        if (window.location.protocol == "https:") {
          protocol = "wss";
        }
        url = `${protocol}://127.0.0.1:8080/websocket/taskProcess/`+username;

        // 打开一个websocket
        websocket = new WebSocket(url);
        // 建立连接
        websocket.onopen = () => {
          // 发送数据
          websocket.send("发送数据");
          console.log("websocket发送数据中");
        };
        // 客户端接收服务端返回的数据
        websocket.onmessage = evt => {
          var data = JSON.parse(evt.data)
          this.todoNum = data.length
          console.log("websocket返回的数据：", evt);
        };
        // 发生错误时
        websocket.onerror = evt => {
          console.log("websocket错误：", evt);
        };
        // 关闭连接
        websocket.onclose = evt => {
          console.log("websocket关闭：", evt);
        };
      }
    }

  }
}
</script>

